package com.qualityevaluationclouduser.service.impl;

import com.qualityevaluationclouduser.entity.RoleFunction;
import com.qualityevaluationclouduser.entity.RoleInfo;
import com.qualityevaluationclouduser.repository.RoleFunctionRepository;
import com.qualityevaluationclouduser.repository.RoleInfoRepository;
import com.qualityevaluationclouduser.service.RoleInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @program: qualityEvaluationCloudProject
 * @description: 角色信息服务实现类
 * @author:
 * @create:
 **/
@Transactional
@Service
public class RoleInfoServiceImpl implements RoleInfoService {

    @Autowired
    private RoleInfoRepository roleInfoRepository;
    @Autowired
    private RoleFunctionRepository roleFunctionRepository;

    @Override
    public RoleInfo addRoleInfo(RoleInfo roleInfo) {
        RoleInfo result = new RoleInfo();
        try {
            boolean isExist = roleInfoRepository.existsRoleInfoByRolename(roleInfo.getRolename());
            if (isExist)
                throw new RuntimeException("角色信息已存在");
            roleInfo.setCreationdate(new Date());
            result = roleInfoRepository.save(roleInfo);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
        return result;
    }

    @Override
    public void delRoleInfo(Long id) {
        try {
            RoleInfo roleInfo = roleInfoRepository.findById(id).get();
            if (null != roleInfo) {
                roleInfoRepository.delete(roleInfo);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }

    @Override
    public RoleInfo updRoleInfo(RoleInfo roleInfo) {
        return roleInfoRepository.saveAndFlush(roleInfo);
    }

    @Override
    public Page<RoleInfo> pageQueryRoleInfoList(int page, int size) {
        Page<RoleInfo> all = roleInfoRepository.findAll(PageRequest.of(page-1, size));
        return all;
    }

    @Override
    public void delRoleInfosByIdIn(List<Long> ids) {
        roleInfoRepository.deleteRoleInfosByIdIn(ids);
    }

    @Override
    public Page<RoleInfo> pageQueryRoleInfoByName(String name, int page, int size) {
        Specification querySpecification = (Specification<RoleInfo>) (root,query,cb) ->{
            List<Predicate> predicates = new ArrayList<Predicate>();
            if (null!=name&&!"".equals(name))
            {
                predicates.add(cb.like(root.get("rolename").as(String.class),"%"+name+"%"));
            }
            return cb.and(predicates.toArray(new Predicate[predicates.size()]));
        };
        return roleInfoRepository.findAll(querySpecification,PageRequest.of(page-1, size));
    }

    @Override
    public List<RoleInfo> findRoleInfoList() {
        return roleInfoRepository.findAll();
    }

    @Override
    public void addDefaultAuthoritys(Long roleId, Long[] MenuInfoIdList) {
        roleFunctionRepository.deleteAllByRoleid(roleId);
        for (Long menuInfoId : MenuInfoIdList) {
            RoleFunction roleFunction = new RoleFunction();
            roleFunction.setMenuid(menuInfoId);
            roleFunction.setRoleid(roleId);
            roleFunction.setCreationdate(new Date());
            roleFunctionRepository.save(roleFunction);
        }
    }
}
